import os
import requests
from bs4 import BeautifulSoup


# 批量爬取百度图片
url = 'https://image.baidu.com/search/albumsdata'
params = {
  'pn': 30,
  'rn': 30,
  'tn': 'albumsdetail',
  'word': '宠物图片',
  'album_tab': '动物',
  'album_id': 688,
  'ic': 0,
  'curPageNum': 1
}
pic_url = requests.get(url,params = params)
linkData = pic_url.json()['albumdata']['linkData']
for link in linkData:
  print(link['thumbnailUrl'])
  name = os.path.basename(link['thumbnailUrl'])
  f = open(f'pic/{name}.jpg','wb')
  img = requests.get(link['thumbnailUrl'])
  f.write(img.content)
f.close()